home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / TIFFframes / ToTIFF.m < prev    next >
Text File  |  1995-06-12  |  8KB  |  300 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "ToTIFF.h"
  5. #import <appkit/Form.h>
  6. #import <streams/streams.h>
  7. #import <appkit/Application.h>
  8. #import <appkit/OpenPanel.h>
  9. #import <appkit/NXImage.h>
  10. #import <appkit/NXBitmapImageRep.h>
  11. #import <appkit/Button.h>
  12. #import <appkit/Control.h>
  13. #import <appkit/SavePanel.h>
  14. #import <appkit/tiff.h>
  15. #import <dpsclient/psops.h>
  16. #import <dpsclient/wraps.h>
  17. #import <sys/param.h>
  18. #import <stdio.h>
  19. #import <stdlib.h>
  20. #import <strings.h>
  21.  
  22.  
  23. @implementation ToTIFF
  24.  
  25. #define WIDTH        256
  26. #define HEIGHT        256
  27. #define    BITMAPDIRECTORY    "Library/Images/bitmaps"
  28. #define    REDFILE        "b7"
  29. #define    GREENFILE    "b4"
  30. #define BLUEFILE    "b2"
  31. #define STARTFRAME    1
  32. #define ENDFRAME    40
  33. #define LZW        0
  34. #define PACKBITS    1
  35. #define    JPEG        2
  36. #define    NONE        3
  37. #define FALSE        0
  38. #define TRUE        !FALSE
  39.  
  40. void prefixdirectory(char *dirname, char *filename)
  41. {
  42.     char *tmp;
  43.     tmp = (char *)malloc(200);
  44.     strcpy(tmp, dirname);
  45.     if(strcmp(&tmp[strlen(tmp)-1],"/"))  strcat(tmp,"/");
  46.     strcat(tmp, filename);
  47.     strcpy(filename, tmp);
  48.     free(tmp);
  49. }
  50.  
  51. void postfixframe(char *filename, int frameInt)
  52. {
  53.     char *frameString;
  54.     frameString = (char *)malloc(6);
  55.     if(strcmp(&filename[strlen(filename)-1],"."))  strcat(filename,".");
  56.     sprintf(frameString, "%d", frameInt);
  57.     strcat(filename, frameString);
  58.     free(frameString);
  59. }
  60.  
  61. unsigned char *loadbyteimage(char *imagename, int *bytecount)
  62. {
  63.     NXStream *stream;
  64.     char *streambuffer=NULL;
  65.     int length, maxlength;
  66.     if ((stream = NXMapFile(imagename, NX_READONLY)) != NULL) {
  67.             NXGetMemoryBuffer(stream, &streambuffer, &length, &maxlength);
  68.         *bytecount = length;
  69.         NXClose(stream);
  70.         return((unsigned char *)streambuffer);
  71.     } else {
  72.             NXRunAlertPanel("Error Opening File",
  73.                 "Filename: %s","OK",NULL,NULL,imagename);
  74.         return(NULL);
  75.     }
  76. }
  77.  
  78.  
  79. unsigned char *equalizebyteimage(unsigned char *buffer, int bytecount)
  80. {
  81.     long int histogram[256], fill;
  82.     int i=0;
  83.     unsigned char *cursor;
  84. /* point to the start of the image data  */
  85.     cursor=buffer;
  86. /* zero the histogram array  */
  87.     for(i=0;i<256;i++)   histogram[i]=0L;
  88. /* count up each byte value and store it in the histogram array  */
  89.     i=0;
  90.     while(i++<bytecount) histogram[(unsigned int)*(cursor++)]++;
  91. /* compute the cumulative histogram  */
  92.     for(i=1;i<256;i++)   histogram[i]=histogram[i]+histogram[i-1];
  93. /* Check that the total counts are correct!!!          */
  94. /* If so, then normalize the cumulative histogram and  */
  95. /*      compute and return the equalized buffer;       */
  96. /*  if not, then alert the user.                       */
  97.     if(histogram[255]==bytecount) {
  98.         fill=histogram[0];
  99.         for(i=0;i<256;i++)histogram[i]=
  100.             255*(histogram[i]-fill)
  101.             /(histogram[255]-fill);
  102.         cursor=buffer; 
  103.         i=0;
  104.         while(i++<bytecount){
  105.            *cursor=(unsigned char)histogram[(unsigned int)*cursor]; 
  106.             cursor++;
  107.         }
  108.         return(buffer);
  109.     }  else {
  110.         NXRunAlertPanel("Error in Cumulative Histogram count",
  111.                 "bytecount=%d != histogram[255]= %d",
  112.                 "OK",NULL,NULL,bytecount, histogram[255]);
  113.         return(NULL);
  114.     }
  115. }
  116.  
  117. - setDefaults:sender
  118. {
  119.  
  120.     char *home, *bitmapdirectory;
  121.     home = (char *)malloc(100);
  122.     bitmapdirectory = (char *)malloc(200);
  123.     strcpy(home,getenv("HOME"));
  124.     strcpy(bitmapdirectory, BITMAPDIRECTORY);
  125.     prefixdirectory(home, bitmapdirectory);
  126. /* initialize bitmapinfo table with default values  */
  127.     [bitmapinfo setIntValue:WIDTH at:0];
  128.     [bitmapinfo setIntValue:HEIGHT at:1];
  129.     [bitmapinfo setStringValue:bitmapdirectory at:2];
  130.     [bitmapinfo setStringValue:REDFILE at:3];
  131.     [bitmapinfo setStringValue:GREENFILE at:4];
  132.     [bitmapinfo setStringValue:BLUEFILE at:5];
  133.     [bitmapinfo setIntValue:STARTFRAME at:6];
  134.     [bitmapinfo setIntValue:ENDFRAME at:7];
  135. /* initialize compression type with default value   */
  136.     compression = NX_TIFF_COMPRESSION_JPEG;
  137.     [compressiontype selectCellAt:JPEG:0];
  138. /* initialize equalize switch with default value   */
  139.     equalize = FALSE;
  140.     [equalizeswitch setState:equalize];
  141.     return self;
  142. }
  143.  
  144. - doLZW:sender
  145. {
  146.     compression = NX_TIFF_COMPRESSION_LZW;
  147.     [compressiontype selectCellAt:LZW:0];
  148.     return self;
  149. }
  150.     
  151. - doPACKBITS:sender
  152. {
  153.     compression = NX_TIFF_COMPRESSION_PACKBITS;
  154.     [compressiontype selectCellAt:PACKBITS:0];
  155.     return self;
  156. }
  157.  
  158. - doJPEG:sender
  159. {
  160.     compression = NX_TIFF_COMPRESSION_JPEG;
  161.     [compressiontype selectCellAt:JPEG:0];
  162.     return self;
  163. }
  164.  
  165. - doNONE:sender
  166. {
  167.     compression = NX_TIFF_COMPRESSION_NONE;
  168.     [compressiontype selectCellAt:NONE:0];
  169.     return self;
  170. }
  171.  
  172.  
  173. - doEQUALIZE:sender
  174. {
  175.     equalize = ![equalizeswitch state];
  176.     [equalizeswitch setState:equalize];
  177.     return self;
  178. }
  179.  
  180. - toTIFFMethod:sender
  181. {
  182.         char filename[MAXPATHLEN+1], tempfilename[MAXPATHLEN+1];
  183.     id myImage;
  184.     unsigned char *myImagePlanes[3];
  185.     int width, height, startframe, endframe, currentframe;
  186.     int bytecount, redbytes=0, greenbytes=0, bluebytes=0;
  187.     char *bitmapsdirectory, *redfile, *greenfile, *bluefile;
  188.     char *tempredfile, *tempgreenfile, *tempbluefile;
  189.     unsigned char *redbuffer=NULL, *greenbuffer=NULL, *bluebuffer=NULL;
  190.     
  191. /* allocate space for strings  */
  192.     bitmapsdirectory = (char *)malloc(100);
  193.     redfile = (char *)malloc(200);
  194.     greenfile = (char *)malloc(200);
  195.     bluefile = (char *)malloc(200);
  196.     tempredfile = (char *)malloc(200);
  197.     tempgreenfile = (char *)malloc(200);
  198.     tempbluefile = (char *)malloc(200);
  199.  
  200.  
  201. /* store user's table entries into variables     */
  202.     width = [bitmapinfo intValueAt:0];
  203.     height = [bitmapinfo intValueAt:1];
  204.     strcpy(bitmapsdirectory, [bitmapinfo stringValueAt:2]);
  205.     strcpy(redfile, [bitmapinfo stringValueAt:3]);
  206.     strcpy(greenfile,[bitmapinfo stringValueAt:4]);
  207.     strcpy(bluefile, [bitmapinfo stringValueAt:5]);
  208.     startframe = [bitmapinfo intValueAt:6];
  209.     endframe = [bitmapinfo intValueAt:7];
  210.  
  211. /* prefix redfile, greenfile and bluefile with bitmapsdirectory  */
  212.      prefixdirectory(bitmapsdirectory, redfile);
  213.     prefixdirectory(bitmapsdirectory,  greenfile);
  214.     prefixdirectory(bitmapsdirectory, bluefile);
  215.     
  216.        bytecount = width*height;
  217.     
  218. /*  read in the frames, beginning with startframe, ending with endframe    */
  219.     currentframe = startframe;
  220.     while(currentframe <= endframe) {
  221.     
  222. /*  tack the current frame number onto the end of each filename   */
  223.         strcpy(tempredfile, redfile);
  224.         strcpy(tempgreenfile, greenfile);
  225.         strcpy(tempbluefile, bluefile);
  226.         postfixframe(tempredfile, currentframe);
  227.         postfixframe(tempgreenfile, currentframe);
  228.         postfixframe(tempbluefile, currentframe);
  229.     
  230. /* read in the bitmap files for this particular frame  */    
  231. /*         and put bytes into 3 buffers                */
  232.         if((redbuffer = loadbyteimage(tempredfile, &redbytes))==NULL)
  233.             return self;
  234.         if((greenbuffer = loadbyteimage(tempgreenfile, &greenbytes))==NULL)
  235.             return self;
  236.         if((bluebuffer = loadbyteimage(tempbluefile, &bluebytes))==NULL)
  237.             return self;
  238.     
  239.     
  240.         if ((redbytes==bytecount)&&(greenbytes==bytecount)&&(bluebytes==bytecount))
  241.         {
  242.     
  243.     /* apply appropriate stretch, if requested  */
  244.         if(equalize) {
  245.         if(equalizebyteimage(redbuffer, redbytes)==NULL) return self;
  246.         if(equalizebyteimage(greenbuffer,greenbytes)==NULL)return self;
  247.         if(equalizebyteimage(bluebuffer, bluebytes)==NULL)return self;
  248.         }
  249.  
  250.     
  251.     /* assign them to myImagePlanes     */
  252.             myImagePlanes[0] = (unsigned char *)redbuffer;
  253.             myImagePlanes[1] = (unsigned char *)greenbuffer;
  254.             myImagePlanes[2] = (unsigned char *)bluebuffer;
  255.     
  256.             myImage = [[NXBitmapImageRep alloc]
  257.             initDataPlanes:myImagePlanes
  258.                 pixelsWide:width
  259.                 pixelsHigh:height
  260.                 bitsPerSample:8
  261.                 samplesPerPixel:3
  262.                 hasAlpha:NO
  263.                 isPlanar:YES
  264.                 colorSpace:NX_RGBColorSpace
  265.                 bytesPerRow:width
  266.                 bitsPerPixel:8];
  267.     
  268.              if (myImage)  {
  269.                 NXStream *s = NXOpenMemory (NULL, 0, NX_READWRITE);
  270.                 if(currentframe == startframe) {
  271.                     id mySavePanel = [SavePanel new];
  272.                         [mySavePanel runModal];
  273.                         if([mySavePanel filename]) strcpy(filename,[mySavePanel filename]);
  274.                 }
  275.                 strcpy(tempfilename, filename);
  276.                 postfixframe(tempfilename, currentframe);
  277.                       strcat(tempfilename,".tiff");
  278.                       if (s) {
  279.                         [myImage writeTIFF:s usingCompression:compression];
  280.                         NXFlush (s);
  281.                         if (NXSaveToFile (s, tempfilename))
  282.                               NXRunAlertPanel("Error Saving File",
  283.                            "Filename: %s","OK",NULL,NULL,filename);
  284.                         NXCloseMemory (s, NX_FREEBUFFER);
  285.                       }
  286.                 }
  287.                 [myImage free];
  288.         }  else {
  289.             NXRunAlertPanel("Incorrect byte count !",
  290.             "%d in red, %d in blue, %d in green.\nAll should = width*height = %d",
  291.             "OK",NULL,NULL,redbytes, greenbytes, bluebytes, bytecount);
  292.         }
  293.         currentframe++;
  294.     }
  295.       return self;
  296. }
  297.  
  298.  
  299. @end
  300.